﻿#pragma execution_character_set("utf-8")
#include "moveoperation.h"
#include <QApplication>
#include "hmicommondefine.h"
#pragma execution_character_set("utf-8")
MoveOperation::MoveOperation(GlobalData *globalDataIn, int allowMaxJogTime)
{
    globalData=globalDataIn;
    continuouseMoveCount=0;
    allowMaxJogCount=allowMaxJogTime*1000/JOG_TIME;
    //寸动
    jogFirst = true;
    jogAxisKeyId = EM_AXIS_1;
    direction = EM_DIRECTION_LEFT;
    jogTimer = new QTimer;
    connect(jogTimer,SIGNAL(timeout()),this,SLOT(jogTimeWork()));

    //点移动
    pointFirst = true;
    pointTimer = new QTimer;
    connect(pointTimer,SIGNAL(timeout()),this,SLOT(pointTimeWork()));
}

bool MoveOperation::isJogTimerActive()
{
    return jogTimer->isActive();
}

void MoveOperation::startJogMove(int keyIdIn, int directionIn)
{
    continuouseMoveCount=0;
    this->jogAxisKeyId = keyIdIn;
    this->direction = directionIn;
    jogTimer->start(JOG_TIME);
    qDebug()<<"startJogMove"<<keyIdIn<<directionIn;
}

void MoveOperation::stopJogMove()
{
    continuouseMoveCount=0;
    jogTimer->stop();
    emit stopWatchDogSignal();
    if(!jogFirst){
//        emit stopRunSignal();//会调用停止程序，导致手动轴时迅速停止。
        qDebug()<<"MoveOperation::stopJogMove";
        emit ceaseRunSignal();
    }
    jogFirst = true;
    qDebug()<<"stopJogMove2";
}

//void MoveOperation::setJogFlag()
//{
//    qDebug()<<"setJogFlag";
//    jogFirst=false;
//}

void MoveOperation::startPointMove(MoveParameter movePoint)
{
    this->movePoint = movePoint;
    pointTimer->start(JOG_TIME);
}

void MoveOperation::stopPointMove()
{
    pointTimer->stop();
    emit stopWatchDogSignal();
    if(!pointFirst){
//        emit stopRunSignal();//会调用停止程序，导致手动轴时迅速停止。
        emit ceaseRunSignal();
    }
    pointFirst = true;
}

void MoveOperation::warmStopMove()
{
    //清 寸动
    stopJogMove();
    //清 点移
    stopPointMove();
}

void MoveOperation::setMaxJogTimeSlot(int timeIn)
{
    qDebug()<<"MoveOperation::setMaxJogTimeSlot"<<timeIn;
    allowMaxJogCount=timeIn*1000/JOG_TIME;
}

void MoveOperation::jogTimeWork()
{
    qDebug()<<"jogTimeWork";
    int tmpJogId=jogAxisKeyId;
    if(globalData)
    {
        if(jogFirst)
        {
                #ifndef D_FOR_DEMO
                if(!globalData->globalLoginStatus)//确保是登录状态下 才能操作
                {
                    stopJogMove();
                    return;
                }


                if(globalData->globalCoordinate == EM_JOG_COORDINATE_JOINT)//关节判断
                {
                    //后累计算起
                    tmpJogId=jogAxisKeyId+globalData->onePageAxisCount*globalData->jogPageIndex;
                    if(globalData->robotAxisCount <= tmpJogId)
                    {
                        stopJogMove();
                        //sleep(1);
                        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("此关节轴不存在"));
                        return;
                    }

                }
                else//笛卡尔判断(某些机型的限制)
                {
                    //二轴 三轴 未处理
                    if(globalData->robotAxisCount == 4)//四轴机器人
                    {
                        if(EM_AXIS_4 == jogAxisKeyId || EM_AXIS_5 == jogAxisKeyId)
                        {
                            stopJogMove();
                            //sleep(1);
                            emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("此轴不可操作"));
                            return;//世界坐标下:四 五 不可操作
                        }
                    }
                    else if(globalData->robotAxisCount == 5)//五轴机器人
                    {
                        if(EM_AXIS_4 <= jogAxisKeyId)
                        {
                            stopJogMove();
                            //sleep(1);
                            emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("此轴不可操作"));
                            return;
                        }
                    }
                }


                if(globalData->globalHandState > EM_CONTROL_MODE_T2) // HUALEI　华磊修改
                {
                    stopJogMove();
                    //sleep(1);
                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("当前模式不可操作\n寸动请切换手动T1模式"));
                    return;
                }
                if(globalData->globalCurrentState == ENUM_COMMAND_RUN || globalData->globalCurrentState == ENUM_COMMAND_CEASE)
                {
//                    stopJogMove();
//                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("当前机器运行中,请按停止按键后，再次寸动"));
                    qDebug()<<"当前机器运行中,请按停止按键后，再次寸动";

                    return;
                }

                if( globalData->globalServoState != EM_MOTOR_STATUS_SERVO_ON)
                {
                    stopJogMove();
                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("伺服没有启动"));

                    return;
                }
                #endif

                emit moveJogSignal(tmpJogId,direction);
                jogFirst = false;
        }
        else
        {
            qDebug()<<"emit runningDog() continuouseMoveCount"<<continuouseMoveCount;
            emit runningDog();
            #ifndef D_FOR_DEMO
            //增加连续手动时间限制判断，
            continuouseMoveCount++;
            if(continuouseMoveCount>allowMaxJogCount)
            {
                stopJogMove();
                QString tmpNote=tr("强制暂停手动!连续手动时间超过限制");
                tmpNote+=QString::number(allowMaxJogCount*JOG_TIME/1000)+"秒";
                emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tmpNote);
                return;
            }
             #endif
        }
    }
}

void MoveOperation::pointTimeWork()
{
    if(globalData)
    {
        if(pointFirst)
        {
//            if(globalData->tryLock(TRYLOCKTIME))
//            {
                if(globalData->globalHandState > EM_CONTROL_MODE_T2)//HUALEI　华磊修改
                {
                    pointTimer->stop();
                    //sleep(1);
//                    globalData->unlock();

                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("当前模式不可操作\n寸动请切换手动T1模式"));

                    return;
                }
                if(globalData->globalCurrentState == ENUM_COMMAND_RUN || globalData->globalCurrentState == ENUM_COMMAND_CEASE)
                {
                    pointTimer->stop();
                    //sleep(1);
//                    globalData->unlock();

                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("当前机器运行中，请按停止按键后，再次移动到点"));

                    return;
                }
                if( globalData->globalServoState != EM_MOTOR_STATUS_SERVO_ON)
                {
                    pointTimer->stop();
                    //sleep(1);
//                    globalData->unlock();

                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("伺服没有启动"));

                    return;
                }
//                globalData->unlock();

                emit movePointSignal(movePoint);
                pointFirst = false;
//            }
        }
        else
        {
            emit runningDog();
        }
    }
}

void MoveOperation::changeLanguageSlot(int language)
{
    if(1 == language)
    {
        translator.load(":/language/HmiCommunicate.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
}
